package com.atguigu.spzx.user.service.impl;

import com.atguigu.common.utils.ThreadLocalUtil;
import com.atguigu.spzx.model.entity.user.UserAddress;
import com.atguigu.spzx.user.mapper.UserAddressMapper;
import com.atguigu.spzx.user.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class userAddressServiceImpl implements UserAddressService {

    @Autowired
    UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> findUserAddressList() {

        Long id = ThreadLocalUtil.getUserInfo().getId();
        return userAddressMapper.findUserAddressList(id);
    }
    @Transactional
    @Override
    public void addAddress(UserAddress userAddress) {
        Long userid = ThreadLocalUtil.getUserInfo().getId();
        userAddress.setUserId(userid);

       String pcdName =  userAddressMapper.getPcdName(userAddress.getDistrictCode());
        String fullAddress = pcdName +userAddress.getAddress() ;
        userAddress.setFullAddress(fullAddress);



        if (userAddress.getIsDefault().intValue() ==1){
            userAddressMapper.updateAddressIsdefault(userid);
        }

        userAddressMapper.addAddress(userAddress);
    }
}
